Shader "UI/BlurBackground"
{
    Properties
    {
        _BlurRadius ("Blur Radius", Range(0, 10)) = 3
        _DownSample ("Down Sample", Range(1, 8)) = 2
    }

    SubShader
    {
        Tags { "Queue"="Transparent" "RenderType"="Opaque" }

        CGINCLUDE
        #include "UnityCG.cginc"

        sampler2D _MainTex;
        float4 _MainTex_TexelSize;
        float _BlurRadius;
        int _DownSample;

        struct v2f_blur
        {
            float4 pos : SV_POSITION;
            float2 uv : TEXCOORD0;
        };

        v2f_blur vert_blur (appdata_img v)
        {
            v2f_blur o;
            o.pos = UnityObjectToClipPos(v.vertex);
            o.uv = v.texcoord.xy;
            return o;
        }

        half4 frag_blur (v2f_blur i) : SV_Target
        {
            float2 uv = i.uv;
            half4 color = tex2D(_MainTex, uv);
            
            // 高斯模糊采样
            float2 texelSize = _MainTex_TexelSize.xy * _DownSample;
            float2 offsets[9] = {
                float2(-1, 1), float2(0, 1), float2(1, 1),
                float2(-1, 0), float2(0, 0), float2(1, 0),
                float2(-1, -1), float2(0, -1), float2(1, -1)
            };

            float kernel[9] = {
                0.0947416, 0.118318, 0.0947416,
                0.118318,  0.147761, 0.118318,
                0.0947416, 0.118318, 0.0947416
            };

            color = 0;
            for(int j = 0; j < 9; j++)
            {
                float2 uvOffset = uv + offsets[j] * texelSize * _BlurRadius;
                color += tex2D(_MainTex, uvOffset) * kernel[j];
            }
            return color;
        }
        ENDCG

        Pass
        {
            CGPROGRAM
            #pragma vertex vert_blur
            #pragma fragment frag_blur
            ENDCG
        }
    }
}
